package com.lib.utils;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import java.util.LinkedList;

/**
 * @author FengLing
 * Created on 2016-7-22 21:59
 * Activity Lifecycle的相关工具类
 */
public final class ActivityLifecycleUtils {
    /**
     * 使用 LinkedList 保存每个 Activity , 增删 速度快
     */
    public static LinkedList<Activity> sList;
    /**
     * 当前顶层动的activity
     */
    private static Activity sActivity;
    private static boolean sShowFragmentLifecycle;

    // 工具类不允许实例化
    private ActivityLifecycleUtils() {
        throw new NullPointerException("来个空指针~");
    }

    public static void init(Application application, boolean showFragmentLifecycle) {
        if (sList != null) {
            return;
        }
        sShowFragmentLifecycle = showFragmentLifecycle;
        sList = new LinkedList<>();
        application.registerActivityLifecycleCallbacks(new InnerCallbacks());
    }

    public static Activity getTopActivity() {
        return sActivity;
    }

    public static void finishOthers(Activity retainActivity) {
        for (Activity activity : sList) {
            if (activity != retainActivity) {
                activity.finish();
            }
        }
    }

    /**
     * 退出整个应用程序
     */
    public static void finishAll() {
        if (sList == null) {
            return;
        }
        int size = sList.size();
        for (int i = 0; i < size; i++) {
            Activity activity = sList.pollFirst();
            if (activity != null) {
                activity.finish();
            }
        }
        XLog.i("finishAll->" + sList);
    }

    private static class InnerCallbacks implements Application.ActivityLifecycleCallbacks {
        private FLC mFLC;

        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            XLog.i("onActivityCreated:" + activity);
            sList.add(activity);

            if (sShowFragmentLifecycle && activity instanceof FragmentActivity) {
                if (mFLC == null) {
                    mFLC = new FLC();
                }
                FragmentActivity fa = (FragmentActivity) activity;

                fa.getSupportFragmentManager().registerFragmentLifecycleCallbacks(mFLC, true);
                XLog.i("注册了Fragment监听");
            }
        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {
            XLog.i("onActivityResumed:" + activity);
            sActivity = activity;
        }

        @Override
        public void onActivityPaused(Activity activity) {
            XLog.i("onActivityPaused:" + activity);
            if (activity == sActivity) {
                sActivity = null;
            }
        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            XLog.i("onActivityDestroyed:" + activity.toString());
            sList.remove(activity);

            if (sShowFragmentLifecycle && activity instanceof FragmentActivity) {
                FragmentActivity fa = (FragmentActivity) activity;
                fa.getSupportFragmentManager().unregisterFragmentLifecycleCallbacks(mFLC);
                XLog.i("注销了Fragment监听");
            }
        }
    }

    private static class FLC extends FragmentManager.FragmentLifecycleCallbacks {

        @Override
        public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment f) {
            String str = toStr(f);
            XLog.i("f--->" + str);
        }

        @Override
        public void onFragmentPaused(@NonNull FragmentManager fm, @NonNull Fragment f) {
            String str = toStr(f);
            XLog.i("f--->" + str);
        }

        @Override
        public void onFragmentViewDestroyed(@NonNull FragmentManager fm, @NonNull Fragment f) {
            String str = toStr(f);
            XLog.i("f--->" + str);
        }
    }

    public static String toStr(Fragment f) {
        return f.getClass().getName() + "@" + Integer.toHexString(f.hashCode()) + "[" + f.getTag() + "]";
    }
}